package main

import (
	"log"
	"fmt"
	"os"
	"net"
)

const PMAP_ADDR = "pmap.onething.net"
const REDIS_CONFIG_TMEPLATE = `
cluster-announce-ip %s
cluster-announce-port %d
cluster-announce-bus-port %d
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
`
func createRedisConfig(port, busPort int) {
	// 解析域名
	ip, e := net.LookupIP(PMAP_ADDR)
	if e != nil {
		log.Print(e)
		panic(0)
	}
	ipAddr := fmt.Sprintf("%s", ip[0])

	// 创建配置文件
	file, err := os.Create("/tmp/redis.conf")
	if err != nil {
		log.Print(err)
		panic(0)
	}

	// 写入配置文件
	config := fmt.Sprintf(REDIS_CONFIG_TMEPLATE, ipAddr, port, busPort)
	if _, err := file.WriteString(config); err != nil {
		log.Print(err)
		panic(0)
	}
}

func main() {
	createRedisConfig(40000, 50000)
}
